﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Composite.Regions;
using EQual.Infrastructure;

namespace EQual.MainModule.Documents.Forms
{
    public class FormsController : EQual.Infrastructure.MVPFramework.Presenters.ControllerBase<IDocumentFormsView,DataAccesLayer .Form  >, EQual.Infrastructure.IModuleController 
   {
       DataAccesLayer.DocumentDataAccesLayer dataLayer;

       EQual.MainModule.Documents.Forms.FormsPresentationModel ViewModel;

       IRegionManager _regionManager;

       public FormsController(IRegionManager regionManager)
           : this()
       {
           this._regionManager = regionManager;
       }

       public FormsController()
       {
           this.View = new DocumentFormsView();

           this.dataLayer = new DataAccesLayer.EQualDataLayerFactory().GetDocumentAccesLayer();
       }
 
       public DataAccesLayer.Document ActiveItem { get; set; }

       internal void DispalyDocumentForms(DataAccesLayer.Document document)
       {
           this.ActiveItem = document;
           this.View.Model = dataLayer.GetDocumentFormsAndRecords(document.Id);           
       }

       internal void AddNewAssociation(DataAccesLayer.Form  form_Record)
       {
           dataLayer.AddNewFromRecordToDocument(form_Record, ActiveItem);
       }

       #region IModuleController Members

       public void Run()
       {
           ViewModel = new EQual.MainModule.Documents.Forms.FormsPresentationModel();
           View.Model = ViewModel;

           this._regionManager.RegisterViewWithRegion(RegionNames.MainRegion, () => this.View);
           this._regionManager.Regions[RegionNames.MainRegion].Activate(this.View);
       }

       #endregion
   }
}
